using System;
using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using UnityEngine.Playables;
using Yoozoo.HRP.Runtime;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    public class BeginBlackFade : LatentActionNodeBase
    {
        public override bool allowRoutineQueueing { get { return false; } }
        private bool complete = false;
        
        // [HideInInspector]
        // public FlowOutput onFinish;
        
        public IEnumerator Invoke(float startAlpha, float endAlpha, float fadeTime)
        {
            complete = false;   
            HCameraManager.BeginBlackFade(startAlpha,endAlpha,fadeTime,OnFadeComplete);
            float t = 0;
            yield return new UnityEngine.WaitUntil(() =>
            {
                t += Time.deltaTime;
                return complete || t > fadeTime;
            });
        }

        private void OnFadeComplete()
        {
            complete = true;
        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            //onFinish = node.AddFlowOutput("onFinish");

            var p1 = node.AddValueInput<float>("startAlpha");
            var p2 = node.AddValueInput<float>("endAlpha");
            var p3 = node.AddValueInput<float>("fadeTime");
            
            node.AddFlowInput("In", (f) => { Begin(Invoke(p1.value,p2.value,p3.value), f); });
        }
    }
}